package com.whl.sm.controller.notice;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.common.util.UploadUtils;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.notice.CommunityNotice;
import com.whl.sm.model.system.Role;
import com.whl.sm.model.system.SysMenu;
import com.whl.sm.service.notice.CommunityNoticeService;
import com.whl.sm.service.system.RoleService;
import com.whl.sm.service.system.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/community-notice")
@Slf4j
public class CommunityNoticeController extends BaseController {

    @Autowired
    private CommunityNoticeService communityNoticeService;

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    RoleService roleService;

    @Value("${ROOT_PATH}")
    private String rootPath;

    @RequestMapping("/list")
    public Result getNoticeList(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("notice_title");
        setHttpRequestQueryWrapper(queryWrapper,"visible","role_type_id","object_type");
        queryWrapper.orderByDesc("update_time");
        queryWrapper.orderByAsc("priority");
        queryWrapper.ne("is_del",1);
        Page<Object> listPage = communityNoticeService.page(getPage(),queryWrapper);
        listPage.getRecords().stream().forEach(obj->{
            CommunityNotice target = (CommunityNotice) obj;
            if (Objects.nonNull(target.getRoleTypeId())){
                Role linkRole = roleService.getById(target.getRoleTypeId());
                target.setRoleName(linkRole.getRoleName());
            }
        });
        return Result.ok(listPage);
    }


    @RequestMapping("/info/{id}")
    public Result noticeInfo(@PathVariable("id") String id){
        CommunityNotice communityNotice = communityNoticeService.getById(id);
        return Result.ok(communityNotice);
    }

    @RequestMapping("/publish")
    public Result publishNotice (@RequestBody CommunityNotice communityNotice){
        return communityNoticeService.publish(communityNotice);
    }


    @RequestMapping("/update")
    public Result updateNotice (@RequestBody CommunityNotice communityNotice){
        return communityNoticeService.update(communityNotice);
    }


    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            communityNoticeService.update(new UpdateWrapper<CommunityNotice>()
                    .eq("id",idList.get(i))
                    .set("repeal_time",new Date())
                    .set("visible",0)
            );
        }
        return Result.ok();
    }


    @RequestMapping("/uploadImg")
    public Result uploadImg(@RequestParam MultipartFile file, HttpServletRequest request){
        String path = "/notice/contentImg";
        try {
            List<Map<String,Object>> fileList = UploadUtils.saveFile(request,path,this.rootPath,"file");
            String resultPath = "";
            if (fileList.size()>0){
                for (Map<String,Object> map : fileList){
                    resultPath = (String) map.get("filePath");
                }
            }
            //System.out.println(resultPath);
            return Result.ok(resultPath);
        }catch (Exception e){
            log.error("公告管理----->公告发布:图片上传，服务器Url转换!");
            return Result.fail();
        }
    }

    @RequestMapping("/getPublishRoute")
    public Result getPublishRoute(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("menu_name","公告发布");
        List<SysMenu> publishMenu = sysMenuService.list(queryWrapper);
        return Result.ok(publishMenu.size()>0?publishMenu.get(0):null);
    }



}
